package com.sk.weichat.ui.lock;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.sk.weichat.R;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.ui.me.SmsCodeForIdentifyActivity;
import com.sk.weichat.util.LiveDataBus;
import com.sk.weichat.view.PasswordInputEditText;
import com.sk.weichat.view.SelectionFrame;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static android.widget.Toast.LENGTH_SHORT;

/**
 * @author lzs
 * @date 2019/9/12
 * Email:343067508@qq.com
 **/
public class ResetDeviceLockActivity extends BaseActivity {

    PasswordInputEditText passwordInputEditText;
    private SelectionFrame selectionFrame;

    private static final int reset_pswd = 7624;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_device_pswd);
        getSupportActionBar().hide();
        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView mTvTitle = (TextView) findViewById(R.id.tv_title_center);
        mTvTitle.setText(getString(R.string.change_device_password));

        passwordInputEditText = findViewById(R.id.edt_pswd);
        passwordInputEditText.setTextWatcher(new Observer<CharSequence>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(CharSequence charSequence) {
                if (charSequence.length() == 6) {
                    passwordInputEditText.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (DeviceLockHelper.getInputErrorTime() >= 1) {
                                selectionFrame.show();
                                passwordInputEditText.setText("");
                                return;
                            }
                            if (DeviceLockHelper.checkPassword(charSequence.toString())) {
                                DeviceLockHelper.unlock();
                                SetDeviceLockActivity.reset(ResetDeviceLockActivity.this, reset_pswd);
                            } else {
                                passwordInputEditText.setText("");
                                //增加输入错误次数
                                DeviceLockHelper.inputErrorTimeIncreate();
                                Toast toast = Toast.makeText(mContext, getString(R.string.tip_device_lock_password_incorrect), LENGTH_SHORT);
                                // toast.setGravity(Gravity.BOTTOM, 0, ViewPiexlUtil.dp2px(mContext, 40));
                                toast.show();
                            }
                        }
                    }, 200);
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
        selectionFrame = new SelectionFrame(mContext);

        selectionFrame.setSomething("温馨提示", getString(R.string.tip_device_lock_password_incorrect_three_time), "取消", "忘记密码", new SelectionFrame.OnSelectionFrameClickListener() {
            @Override
            public void cancelClick() {

            }

            @Override
            public void confirmClick() {
                //忘记密码,跳转验证身份
                SmsCodeForIdentifyActivity.startSmsCodeForIdentifyActivity(ResetDeviceLockActivity.this.hashCode() + "", mContext);
            }
        });

        LiveDataBus.get().with(ResetDeviceLockActivity.this.hashCode() + "", Integer.class).observe(this, new android.arch.lifecycle.Observer<Integer>() {
            @Override
            public void onChanged(@Nullable Integer code) {
                //验证码验证身份通过
                if (code == SmsCodeForIdentifyActivity.code_success) {
                    SetDeviceLockActivity.reset(ResetDeviceLockActivity.this, reset_pswd);
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == reset_pswd && resultCode == Activity.RESULT_OK) {
            //重置密码成功
            finish();
        }
    }
}
